import java.sql.*;

public class CURDJdbcTest {

    static public void travel(ResultSet rs, String format, Object ... obj) throws Exception {
        System.out.printf(format, obj);
        rs.absolute(0);
        while (rs.next()) {
            System.out.printf("%2d %8s %s\n", rs.getInt(1), rs.getString(2), rs.getString(3));
        }
    }

    static public void add(ResultSet rs,
                            String ... field) throws Exception {
        rs.moveToInsertRow();
        int col = rs.getMetaData().getColumnCount();
        for (int i = 1; i <= col; ++i) {
            rs.updateString(i, field[i - 1]);
        }
        rs.insertRow();
    }

    static public void edit(ResultSet rs, int row,
                            String ... field) throws Exception {
        rs.absolute(row);
        rs.updateObject("deptname", "hello");

        /*
         * for (int i = 0; i < field.length; i += 2) {
         *     rs.updateString(field[i], field[i + 1]);
         * }
         */
        // rs.refreshRow();
        rs.updateRow();
    }

    static public void del(ResultSet rs, int row) throws Exception {
        rs.absolute(row);
        rs.deleteRow();
    }

    static public void main(String [] args) throws Exception {
        MyDb db = new MyDb("company", "root", "123123");

        db.query("select * from dept");
        ResultSet rs = db.getResult();
        travel(rs, "select:\n");

        // 添加数据
        // add(rs, "6", "ABC", "ABCL");
        // travel(rs, "=========== add ============\n");

        // 更新数据
        edit(rs, 6, "deptno", "16", "deptname", "admin");
        travel(rs, "=========== edit ============\n");

        // 删除数据
        del(rs, 6);
        travel(rs, "=========== del ============\n");

        db.close();
    }
}
